package com.swas.service.impl;

import com.swas.dao.PatrolPlanMapper;
import com.swas.dao.PlanPointDao;
import com.swas.entity.PatrolPlan;
import com.swas.entity.PatrolPlanExample;
import com.swas.entity.PlanPoint;
import com.swas.service.PatrolPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PatrolPlanServiceImpl implements PatrolPlanService {
    @Autowired
    private PatrolPlanMapper patrolPlanMapper;

    @Autowired
    private PlanPointDao planPointDao;

    @Override
    public List<PatrolPlan> findAll(Integer pageNum, Integer pageSize, String planno, Integer pattype, Integer patcycle, Date startdate, Date enddate) {
        PatrolPlanExample patrolPlanExample = new PatrolPlanExample();
        PatrolPlanExample.Criteria criteria = patrolPlanExample.createCriteria();
        if (planno != null & planno != "") {
            criteria.andPlannoEqualTo(planno);
        }
        if (pattype != null) {
            criteria.andPattypeEqualTo(pattype);
        }
        if (patcycle != null) {
            criteria.andPatcycleEqualTo(patcycle);
        }
        if (startdate != null) {
            criteria.andStartdateGreaterThanOrEqualTo(startdate);
        }
        if (enddate != null) {
            criteria.andEnddateLessThanOrEqualTo(enddate);
        }
        List<PatrolPlan> patrolPlanList = patrolPlanMapper.selectByExample(patrolPlanExample);
        return patrolPlanList;
    }

    @Override
    public boolean addPatrolPlan(PatrolPlan patrolPlan) {
        int i = patrolPlanMapper.insertSelective(patrolPlan);
        //计划巡检表添加
        List<Integer> pointIdList = patrolPlan.getPointIdList();
        for (Integer pointId : pointIdList) {
            PlanPoint planPoint = new PlanPoint();
            planPoint.setPlanid(patrolPlan.getId());
            planPoint.setPointid(pointId);
            planPointDao.insertSelective(planPoint);
        }
        return i > 0 ? true : false;
    }

    @Override
    public PatrolPlan fiandOne(Integer id) {
        PatrolPlan patrolPlan = patrolPlanMapper.selectByPrimaryKey(id);
        return patrolPlan;
    }
}
